<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comment_model extends OC_Model {

	/*comments article*/
	function load_com_articles($params = array())
	{
		$this->db->select('SQL_CALC_FOUND_ROWS a.*,b.a_title', FALSE);
		$this->db->from('comment_article a');
		$this->db->join('articles b', 'a.a_id = b.a_id', 'left');

		if (isset($params['sort'], $params['order']))
			$this->db->order_by($params['sort'], $params['order']);

		if (isset($params['page'], $params['rows']))
			$this->db->limit($params['rows'], (intval($params['page']-1)*intval($params['rows'])));

		if (isset($params['query']) && $params['query'] != '')
		{
			$query = $params['query'];

			$likes = array_map(
				function($f) use ($query) { return "$f LIKE '%$query%'"; }, 
				array('ca_comments','ca_email','ca_fullname','a_title')
			);
			
			$this->db->where('('.implode(' OR ', $likes).')');
		}

		return $this->fetch_rows();
	}

	public function ca_approve($params)
	{
		$this->db->where('ca_id', $params['ca_id']);
		$this->db->set('ca_approved','1');
		return $this->db->update('comment_article');
	}

	public function ca_reject($params)
	{
		$this->db->where('ca_id', $params['ca_id']);
		$this->db->set('ca_approved','2');
		return $this->db->update('comment_article');
	}

	/*comments recipes*/
	function load_com_recipes($params = array())
	{
		$this->db->select('SQL_CALC_FOUND_ROWS a.*,b.r_name', FALSE);
		$this->db->from('comment_recipes a');
		$this->db->join('recipes b', 'a.r_id = b.r_id', 'left');

		if (isset($params['sort'], $params['order']))
			$this->db->order_by($params['sort'], $params['order']);

		if (isset($params['page'], $params['rows']))
			$this->db->limit($params['rows'], (intval($params['page']-1)*intval($params['rows'])));

		if (isset($params['query']) && $params['query'] != '')
		{
			$query = $params['query'];

			$likes = array_map(
				function($f) use ($query) { return "$f LIKE '%$query%'"; }, 
				array('cr_comments','cr_email','cr_fullname','r_name')
			);
			
			$this->db->where('('.implode(' OR ', $likes).')');
		}

		return $this->fetch_rows();
	}

	public function cr_approve($params)
	{
		$this->db->where('cr_id', $params['cr_id']);
		$this->db->set('cr_approved','1');
		return $this->db->update('comment_recipes');
	}

	public function cr_reject($params)
	{
		$this->db->where('cr_id', $params['cr_id']);
		$this->db->set('cr_approved','2');
		return $this->db->update('comment_recipes');
	}

	/*comment events*/
	function load_com_events($params = array())
	{
		$this->db->select('SQL_CALC_FOUND_ROWS a.*,b.e_name', FALSE);
		$this->db->from('comment_events a');
		$this->db->join('events b', 'a.e_id = b.e_id', 'left');

		if (isset($params['sort'], $params['order']))
			$this->db->order_by($params['sort'], $params['order']);

		if (isset($params['page'], $params['rows']))
			$this->db->limit($params['rows'], (intval($params['page']-1)*intval($params['rows'])));

		if (isset($params['query']) && $params['query'] != '')
		{
			$query = $params['query'];

			$likes = array_map(
				function($f) use ($query) { return "$f LIKE '%$query%'"; }, 
				array('ce_comments','ce_email','ce_fullname','e_name')
			);
			
			$this->db->where('('.implode(' OR ', $likes).')');
		}

		return $this->fetch_rows();
	}

	public function ce_approve($params)
	{
		$this->db->where('ce_id', $params['ce_id']);
		$this->db->set('ce_approved','1');
		return $this->db->update('comment_events');
	}

	public function ce_reject($params)
	{
		$this->db->where('ce_id', $params['ce_id']);
		$this->db->set('ce_approved','2');
		return $this->db->update('comment_events');
	}
}